﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using AutoMapper;
using Treinamento2015_01.Models;
using Treinamento2015_01Core.Models;
using Treinamento2015_01Core.Service;

namespace Treinamento2015_01.Controllers
{
    public class ManufacturerController : BaseController
    {

        // GET: /Manufacturer/
        public ActionResult Index()
        {
            return View(Mapper.Map<List<ManufacturerViewModel>>(new ManufacturerService().FindAllActives()));
        }

        // GET: /Manufacturer/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            
            ManufacturerViewModel manufacturerviewmodel = Mapper.Map<ManufacturerViewModel>(new ManufacturerService().FindByKey((int)id));
            
            if (manufacturerviewmodel == null)
            {
                return HttpNotFound();
            }
            return View(manufacturerviewmodel);

        }

        // GET: /Manufacturer/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: /Manufacturer/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="Id,Name")] ManufacturerViewModel manufacturerviewmodel)
        {
            if (ModelState.IsValid)
            {
                Manufacturer manufacturer = Mapper.Map<Manufacturer>(manufacturerviewmodel);

                manufacturer.CreationDate = DateTime.Now;
                manufacturer.ModificationDate = DateTime.Now;
                manufacturer.IsActive = true;

                new ManufacturerService().Create(manufacturer);

                return RedirectToAction("Index");
            }

            return View(manufacturerviewmodel);
        }

        // GET: /Manufacturer/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ManufacturerViewModel manufacturerviewmodel = Mapper.Map<ManufacturerViewModel>(new ManufacturerService().FindByKey((int)id));
            if (manufacturerviewmodel == null)
            {
                return HttpNotFound();
            }
            return View(manufacturerviewmodel);
        }

        // POST: /Manufacturer/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="Id, Name")] ManufacturerViewModel manufacturerviewmodel)
        {
            if (ModelState.IsValid)
            {
                ManufacturerService manufacturerService = new ManufacturerService();
                Manufacturer manufacturer = manufacturerService.FindByKey((int)manufacturerviewmodel.Id);

                manufacturer.ModificationDate = DateTime.Now;
                manufacturer.Name = manufacturerviewmodel.Name;

                manufacturerService.Update(manufacturer);

                return RedirectToAction("Index");
            }
            return View(manufacturerviewmodel);
        }

        // GET: /Manufacturer/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ManufacturerViewModel manufacturerviewmodel = Mapper.Map<ManufacturerViewModel>(new ManufacturerService().FindByKey((int)id));
            if (manufacturerviewmodel == null)
            {
                return HttpNotFound();
            }
            return View(manufacturerviewmodel);
        }

        // POST: /Manufacturer/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            ManufacturerService manufacturerService = new ManufacturerService();
            Manufacturer manufacturer = manufacturerService.FindByKey((int)id);

            manufacturerService.SetAsInactive(manufacturer);

            return RedirectToAction("Index");
        }

    }
}
